package com.geekaca.d06.homework;

import java.util.*;

public class Test3 {
    static Random random = new Random();

    public static void main(String[] args) {
//        Random random = new Random();
        // 创建景点集合
        List<Character> sceneList = new ArrayList<>();
        sceneList.add('A');
        sceneList.add('B');
        sceneList.add('C');
        sceneList.add('D');
        // 创建学生集合
        List<String> studentList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            studentList.add("Student_" + (i + 1));
        }
        // 学生景点选择集合, 学生为 key, 对应景点集合为 value;
        Map<String, ArrayList<Character>> studentChoiceMap = new HashMap<>();
        // 设置每个学生的景点选择集合
        for (int i = 0; i < studentList.size(); i++) {
            studentChoiceMap.put(studentList.get(i), makeChoices(sceneList));
        }
        ArrayList<Character> seceneResList = findMaxScene(studentChoiceMap);

        System.out.println("最热门的景点为: "+ seceneResList);

    }
    // 随机设置每个学生选择的景点
    public static ArrayList<Character> makeChoices(List<Character> sceneList) {
        // 把景点顺序打乱
        Collections.shuffle(sceneList);
        // 随机选择景点数量 1~4
        int randLength = random.nextInt(sceneList.size()) + 1;
//        System.out.println("random num is: " + randLength);
        ArrayList<Character> myChoices = new ArrayList<>();

        for (int i = 0; i < randLength; i++) {
            myChoices.add(sceneList.get(i));
        }
        return myChoices;
    }

    public static ArrayList<Character> findMaxScene(Map<String, ArrayList<Character>> studentChoiceMap) {
        int countA = 0;
        int countB = 0;
        int countC = 0;
        int countD = 0;

        for (ArrayList<Character> sceneList : studentChoiceMap.values()) {
            if (sceneList.contains('A')) {
                countA += 1;
            }
            if (sceneList.contains('B')) {
                countB += 1;
            }
            if (sceneList.contains('C')) {
                countC += 1;
            }
            if (sceneList.contains('D')) {
                countD += 1;
            }
        }
        // 累加每个景点的值放入数组并求出最大值
        int[] myArr = {countA, countB, countC, countD};
        Arrays.sort(myArr); // 数组排序
        System.out.println("最想去景点选择个数为: " + myArr[myArr.length - 1]);
        int max = myArr[myArr.length - 1];
        // 定义最大值集合,有可能存在最多选择的景点数量一致的情况,所以返回集合
        ArrayList<Character> seceneResList = new ArrayList<>();

        if (countA == max) {
            seceneResList.add('A');
        }
        if (countB == max) {
            seceneResList.add('B');
        }
        if (countC == max) {
            seceneResList.add('C');
        }
        if (countD == max) {
            seceneResList.add('D');
        }
        return seceneResList;
    }
}
